define(function (require) {
    require('validate');
    var Base = require('common/base/main');
    var utils = require('common/utils/home');
    var Mask = require('common/mask');
    var mask = new Mask();
    var waterfall = require('common/waterfall');
    require('uploadifive');
    var uploadItemTpl = require('text!group/common/templates/upload_tpl.html');
    require('lazyload');
    require('swiper');

    return Base.extend({
        initialize: function (params) {
            var _this = this;
            Base.prototype.initialize.apply(this, [params]);
            _this.setupItemWaterfall();
            _this.bind_show_url();
            _this.tele_check();
        },
        bind_show_url: function () {
            var _this = this;
            _this.$('.J_show_url').unbind('click').click(function () {
                var $this = $(this);
                layer.load();
                utils.ajaxGet($this.data('url'), function (resp) {
                    layer.closeAll('loading');
                    layer.open({
                        type: 1,
                        title: $this.data('title'),
                        shadeClose: true,
                        shade: 0.8,
                        area: ['100%', '100%'],
                        content: resp.data.info,
                        success: function () {
                            $('.layui-layer').siblings().addClass('layerui-showing');
                        },
                        end: function () {
                            $('.layerui-showing').removeClass('layerui-showing');
                        }
                    });
                });
            });
        },
        setupLazyLoad: function () {
            var _this = this;
            var sel = 'img[data-original!=""]';

            function bind() {
                _this.$(sel).lazyload({
                    effect: 'fadeIn',
                    placeholder: _global.root + 'mall/assets/placeholder',
                });
            }

            bind();
            _this.$(sel + ':lt(5)').trigger('appear');
        },
        bind_submit: function (title, fn) {
            var _this = this;
            if (_.empty(title)) {
                title = "操作成功";
            }
            _this.$('form').validate({
                submitHandler: function (form) {
                    var params = _this.$('form').serializeJson();
                    utils.ajaxPost(_.extend(params), function (data) {
                        mask.success(title, function () {
                            if (fn) {
                                fn();
                            }
                            else {
                                window.history.go(-1);
                            }
                        });
                    });
                }
            });
        },
        setupItemWaterfall: function () {
            var _this = this;
            $('.J_item_img').each(function () {
                $(this).css({
                    width: $(this).parent().width() + 'px',
                    height: $(this).parent().width() + 'px',
                });
            });
            _this.setupLazyLoad();
        },
        waterfall: function (params) {
            var _this = this;
            if (!params) {
                params = {};
            }

            _this.setupItemWaterfall();
            new waterfall({
                fn: function () {
                    _this.setupItemWaterfall();
                    if (params.fn) {
                        params.fn();
                    }
                }
            });
        },
        page_width: function () {
            return $(window).width() > 480 ? 480 : $(window).width();
        },
        initUpload: function (params) {
            var _this = this;

            var img_index = 0;
            $('input[type="file"]').each(function () {
                var $this = $(this);
                var id = $this.attr('id');

                var initParams = {
                    buttonText: '',
                    multi: false,
                    width: 150,
                    dnd: false,
                    fileObjName: id,
                    itemTemplate: _this.render(uploadItemTpl, {}),
                    fileType: ['image/jpeg', 'image/jpg', 'image/png', 'image/bmp'],
                    uploadScript: _.wap_url({m: 'assets', a: 'uploadImage'}),
                    onError: function () {

                    },
                    onSelect: function () {

                    },
                    onUploadComplete: function (file, data) {
                        data = JSON.parse(data);
                        if (data.status == 0) {
                            mask.error(data.msg);
                        }
                        else {
                            file.queueItem.css({
                                'background-image': 'url(' + data.data.fullPath + ')'
                            });
                            $('.uploadifive-hidden', file.queueItem).val(data.data.savePath);

                            $('.uploadifive-hidden', file.queueItem).attr('name', id + (initParams.queueSizeLimit == 1 ? '' : '[' + img_index + ']'));
                            img_index++;
                        }
                        if (params._onUploadComplete) {
                            params._onUploadComplete(file, data);
                        }
                    }
                };
                $this.uploadifive(_.extend(initParams, params));
            });
        },
		tele_check: function(){
			if(_global.tele_invalid == 1){
				layer.open({
					id: 'bindtele_layuipro', //设定一个id，防止重复弹出
					title: '您还没有绑定手机号',
					shade: 0,
					content: '<form action="'+_.U({m:'user',a:'bindtele'})+'" id="bindteleForm">\
							<div class="container">\
								<div class="row">\
									<div class="col-xs-12">手机号码：<span class="bindtele"><input type="number" name="tele"></span></div>\
								</div>\
								<div class="row mt10">\
									<div class="col-xs-12">登录密码：<span class="bindtele"><input type="password" name="password"></span></div>\
								</div>\
								<div class="row mt10">\
									<div class="col-xs-12">确认密码：<span class="bindtele"><input type="password" name="rpassword"></span></div>\
								</div>\
								<div class="row">\
									<div class="col-xs-12 bindtele_error_msg tr red"></div>\
								</div>\
							</div>\
							<input type="hidden" name="m" value="user">\
							<input type="hidden" name="a" value="bindtele">\
						</form>',
					btn: ['绑定', '关闭'],
					shade: 0.8,
					yes: function(){
						var params = $('#bindteleForm').serializeJson();
						utils.ajaxPost(_.extend(params), function (data) {
							if(data.error == 1){
								$('.bindtele_error_msg').html(data.msg);
							}else{
								layer.msg('绑定成功');
							}
						});
					},
					btn2: function(){
						layer.closeAll();
					},
					zIndex: layer.zIndex,
					success: function(layero){
						layer.setTop(layero); //重点2
					}
				});
			}
		}
    });
});